package com.yunfeisoft.controller.search;

import com.applet.utils.Page;
import com.yunfeisoft.business.model.*;
import com.yunfeisoft.business.service.inter.*;
import com.yunfeisoft.model.Attachment;
import com.yunfeisoft.service.inter.AttachmentService;
import com.yunfeisoft.service.inter.DataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 数据库实现移动端信息查询
 */
public class DBSearcher implements SearchManager {

    @Autowired
    private PmsProductService pmsProductService;

    @Autowired
    private SmsHomeAdvertiseService smsHomeAdvertiseService;

    @Autowired
    private SmsFlashPromotionService smsFlashPromotionService;

    @Autowired
    private SmsHomeNewProductService smsHomeNewProductService;

    @Autowired
    private SmsFlashPromotionProductRelationService smsFlashPromotionProductRelationService;

    @Autowired
    private SmsCouponService smsCouponService;

    @Autowired
    private SmsHomeBrandService smsHomeBrandService;

    @Autowired
    private AttachmentService attachmentService;

    @Autowired
    private DataService dataService;

    @Autowired
    private CmsSubjectService cmsSubjectService;

    /**
     * 查询广告列表
     * @param params 查询条件
     * @return
     */
    @Override
    public List<SmsHomeAdvertise> queryAdvertList(Map<String, Object> params) {
        return smsHomeAdvertiseService.queryAdvertList(params);
    }

    /**
     * 查询最新的6个商品
     * @param params 查询条件
     * @return
     */
    @Override
    public List<PmsProduct> queryProductList(Map<String, Object> params) {
        return pmsProductService.queryNewProduct(params, 6);
    }

    /**
     * 根据品牌分页查询商品列表
     * @param params 查询条件
     * @return
     */
    @Override
    public Page<PmsProduct> queryProductPage(Map<String, Object> params) {
        return pmsProductService.queryPageByBrand(params);
    }

    @Override
    public Page<SmsFlashPromotion> queryFlashPromotionList(Map<String, Object> params) {
        return smsFlashPromotionService.queryPage(params);
    }

    /**
     * 分页查询新鲜好物
     * @param params
     * @return
     */
    @Override
    public Page<SmsHomeNewProduct> queryHomeNewProduct(Map<String, Object> params) {
        return smsHomeNewProductService.queryHomeNewProductPage(params);
    }

    /**
     * 查询首页信息
     * @return
     */
    @Override
    public Map<String, Object> queryIndex() {
        Map<String, Object> result = new HashMap<>();
        // 1.查询广告列表
        List<SmsHomeAdvertise> advertiseList = smsHomeAdvertiseService.queryAdvertList(null);
        result.put("advertiseList", advertiseList);
        // 2.查询最新的6个商品
        List<PmsProduct> productList = pmsProductService.queryNewProduct(null, 6);
        result.put("productList", productList);
        // 3.查询新鲜好物商品
        List<SmsHomeNewProduct> smsHomeNewProductList = smsHomeNewProductService.queryHomeNewProductIndex(12);
        result.put("smsHomeNewProductList", smsHomeNewProductList);
        // 4.查询秒杀商品列表
        List<SmsFlashPromotionProductRelation> flashPromotionProductList = smsFlashPromotionProductRelationService.queryFlashPromotionProductIndex(6).stream().limit(6).collect(Collectors.toList());
        result.put("flashPromotionProductList", flashPromotionProductList);
        // 5.查询首页推荐品牌列表
        List<SmsHomeBrand> homeBrandList = smsHomeBrandService.queryHomeBrandIndex(6);
        result.put("homeBrandList", homeBrandList);
        return result;
    }

    /**
     * 查询商品详情
     * @param productId
     * @return
     */
    @Override
    public Map<String, Object> queryProductDetail(String productId) {
        Map<String, Object> result = new HashMap<>();
        // 查询商品信息
        PmsProduct product = pmsProductService.load(productId);
        if (!ObjectUtils.isEmpty(product)) {
            // 根据商品id查询图片列表
            List<Attachment> attachmentList = attachmentService.queryByRefId(productId);
            product.setAttachmentList(attachmentList);
        }
        result.put("product", product);
        // 查询商品满足的优惠券信息
        List<SmsCoupon> couponList = smsCouponService.queryListByProductId(productId);
        result.put("couponList", couponList);
        return result;
    }

    /**
     * 根据商品id查询商品详情
     * @param productId
     * @return
     */
    @Override
    public String queryProductContent(String productId) {
        return dataService.loadByRefId(productId);
    }

    /**
     * 分页查询专题列表
     * @param params
     * @return
     */
    @Override
    public Page<CmsSubject> queryCmsSubjectList(Map<String, Object> params) {
        return cmsSubjectService.queryPage(params);
    }
}
